package com.soyorin.goods.controller;

import com.soyorin.goods.entity.GoodsCategory;
import com.soyorin.goods.service.GoodsCategoryService;
import org.springframework.http.ResponseEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods-category")
public class GoodsCategoryController {

    @Autowired
    private GoodsCategoryService baseService;

    // 标准接口一: select-one
    @GetMapping("/{id}")
    @ResponseBody
    public ResponseEntity<GoodsCategory> selectById(@PathVariable Long id) {
        GoodsCategory GoodsCategory = baseService.getById(id);
        if(GoodsCategory != null) { // return1: 成功查询到数据
            return ResponseEntity.status(200).body(GoodsCategory);
        } else { // return2: 未找到对应数据
            return ResponseEntity.status(404).body(null);
        }
    }

    // 标准接口二: select-all
    @GetMapping
    @ResponseBody
    public PageInfo<GoodsCategory> selectAll(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<GoodsCategory> list = baseService.list();
        return new PageInfo<>(list);
    }

    // 标准接口三: insert
    @PostMapping
    @ResponseBody
    public ResponseEntity<GoodsCategory> insert(@RequestBody GoodsCategory GoodsCategory) {
        boolean result = baseService.save(GoodsCategory);
        if(result) { // return1: 成功插入数据
            return ResponseEntity.status(200).body(GoodsCategory);
        } else { // return2: 插入数据失败
            return ResponseEntity.status(500).body(null);
        }
    }

    // 标准接口四: delete
    @DeleteMapping("/{id}")
    @ResponseBody
    public ResponseEntity<GoodsCategory> delete(@PathVariable Long id) {
        GoodsCategory GoodsCategory = baseService.getById(id);
        if(GoodsCategory != null) {
            boolean result = baseService.removeById(id);
            if(result) { // return1: 成功删除数据
                return ResponseEntity.status(200).body(GoodsCategory);
            } else { // return2: 删除数据失败
                return ResponseEntity.status(500).body(GoodsCategory);
            }
        } else { // return3: 未找到对应数据
            return ResponseEntity.status(404).body(null);
        }
    }

    // 标准接口五: update
    @PutMapping("/{id}")
    @ResponseBody
    public ResponseEntity<GoodsCategory> update(
            @PathVariable Long id,
            @RequestBody GoodsCategory GoodsCategory) {
        GoodsCategory.setId(id);
        boolean result = baseService.updateById(GoodsCategory);
        GoodsCategory updateGoodsCategory = baseService.getById(GoodsCategory.getId());
        if(result && updateGoodsCategory != null) { // return1: 成功更新数据
            return ResponseEntity.status(200).body(updateGoodsCategory);
        } else { // return2: 更新数据失败
            return ResponseEntity.status(500).body(GoodsCategory);
        }
    }

    // 标准接口六: select-eq
    @PostMapping("/select-eq")
    @ResponseBody
    public ResponseEntity<List<GoodsCategory>> selectEq(
            @RequestBody Map<String,Object> conditions) {
        try {
            System.out.println("🔍 收到查询条件: " + conditions);
            
            QueryWrapper<GoodsCategory> wrapper = new QueryWrapper<>();
            for(Map.Entry<String, Object> entry : conditions.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                System.out.println("📊 处理条件: " + key + " = " + value + " (类型: " + value.getClass().getSimpleName() + ")");
                
                // 处理字段名映射：将驼峰命名转换为下划线命名
                String dbFieldName = key;
                if ("relaShopId".equals(key)) {
                    dbFieldName = "rela_shop_id";
                } else if ("innerIndex".equals(key)) {
                    dbFieldName = "inner_index";
                } else if ("createTime".equals(key)) {
                    dbFieldName = "create_time";
                } else if ("updateTime".equals(key)) {
                    dbFieldName = "update_time";
                }
                
                System.out.println("🔄 字段映射: " + key + " -> " + dbFieldName);
                wrapper.eq(dbFieldName, value);
            }
            
            System.out.println("🔍 构建的SQL条件: " + wrapper.getSqlSegment());
            
            List<GoodsCategory> list = baseService.list(wrapper);
            System.out.println("✅ 查询结果数量: " + (list != null ? list.size() : 0));
            
            if(list != null) { // return1: 成功查询到数据
                return ResponseEntity.status(200).body(list);
            } else { // return2: 未找到对应数据
                return ResponseEntity.status(404).body(null);
            }
        } catch (Exception e) {
            System.err.println("❌ 查询过程中发生异常:");
            e.printStackTrace();
            return ResponseEntity.status(500).body(null);
        }
    }

}